#include "weldpropertydialog.h"
#include "ui_weldpropertydialog.h"
#include <QDebug>
#include "hmicommondefine.h"
#include <QString>
#include <QRegExp>

WeldPropertyDialog::WeldPropertyDialog(int styleId,CommunicateNormalDelegate *communicateNormalIn,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WeldPropertyDialog)
{
    ui->setupUi(this);
    move(0,0);
    initialDisplayStyle(styleId);
    communicateNormalDelegate = communicateNormalIn;

}

void WeldPropertyDialog::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);

        ui->returnBtn->setGeometry(500,400,70,40);
        ui->saveBtn->setGeometry(500,350,70,40);

        break;
    default:
        break;
    }
}


void WeldPropertyDialog::updateShowWeldInfo(int index)
{
    //gSoap通信获取焊接属性列表信息
    std::vector<struct WeldProperty> configListOut;
    if(communicateNormalDelegate->getWeldPropertConfigList(configListOut)==1){
     qDebug() << "listCount:" << configListOut.size();
    }
    struct WeldProperty weldProperty;
    weldProperty = configListOut.at(index);
    updateUiShow(weldProperty);

}

WeldPropertyDialog::~WeldPropertyDialog()
{
    delete ui;
}

void WeldPropertyDialog::showView_slot(){
    show();
    updateShowWeldInfo(ui->comboBox->currentIndex());


}

void WeldPropertyDialog::updateUiShow(WeldProperty weldProperty){


    ui->lineEdit->setText(weldProperty.wealdEquipment.data());
    ui->lineEdit_2->setText(weldProperty.equipmentVendors.data());
    ui->lineEdit_3->setText(weldProperty.modelName.data());
    ui->lineEdit_4->setText(weldProperty.fileName.data());
    ui->lineEdit_5->setText(QString("%1").arg(weldProperty.prePlenum));
    ui->lineEdit_6->setText(QString("%1").arg(weldProperty.arcStartingTime));
    ui->lineEdit_7->setText(QString("%1").arg(weldProperty.lagPlenum));
    ui->lineEdit_8->setText(QString("%1").arg(weldProperty.arcExtinguishingTime));

    //启动处理
    if(weldProperty.startTheProcess == 0){
        ui->checkBox->setChecked(false);
    }else {
       ui->checkBox->setChecked(true);
    }

    //后处理
    if(weldProperty.postProcessing == 0){
        ui->checkBox_2->setChecked(false);
    }else {
        ui->checkBox_2->setChecked(true);
    }

    //溶敷解除
    if(weldProperty.solubleApplyRemove == 0){
        ui->checkBox_3->setChecked(false);
    }else {
        ui->checkBox_3->setChecked(true);
    }

    //焊接设定倾斜功能
    if(weldProperty.wealdTilt == 0){
        ui->checkBox_4->setChecked(false);
    }else{
        ui->checkBox_4->setChecked(true);
    }

}

void WeldPropertyDialog::on_returnBtn_clicked()
{
    this->hide();
}

void WeldPropertyDialog::on_saveBtn_clicked()
{

    QString str1 = ui->lineEdit_5->text();
    QString str2 = ui->lineEdit_6->text();
    QString str3 = ui->lineEdit_7->text();
    QString str4 = ui->lineEdit_8->text();

    QString pattern("^[0-9]+(.[0-9]{1,5})?$");
    QRegExp rxRoller(pattern);
    if(!rxRoller.exactMatch(str1) || !rxRoller.exactMatch(str2) || !rxRoller.exactMatch(str3)|| !rxRoller.exactMatch(str4)){
       emit showErrorMsgBox(QString("存在非法字符，请修改！"));
       return;
    }

    qDebug() << "save";

    int index = ui->comboBox->currentIndex();


    struct WeldProperty weldProperty;
    weldProperty.index = index;
    weldProperty.wealdEquipment = ui->lineEdit->text().toStdString();
    weldProperty.equipmentVendors = ui->lineEdit_2->text().toStdString();
    weldProperty.modelName = ui->lineEdit_3->text().toStdString();
    weldProperty.fileName = ui->lineEdit_4->text().toStdString();
    weldProperty.setingNum = 0;
    weldProperty.prePlenum = ui->lineEdit_5->text().toDouble();
    weldProperty.arcStartingTime = ui->lineEdit_6->text().toDouble();
    weldProperty.lagPlenum = ui->lineEdit_7->text().toDouble();
    weldProperty.arcExtinguishingTime = ui->lineEdit_8->text().toDouble();
    if(ui->checkBox->isChecked()){
        weldProperty.startTheProcess = 1;
    }else{
        weldProperty.startTheProcess = 0;
    }

    if(ui->checkBox_2->isChecked()){
        weldProperty.postProcessing = 1;
    }else{
        weldProperty.postProcessing = 0;
    }

    if(ui->checkBox_3->isChecked()){
        weldProperty.solubleApplyRemove = 1;
    }else {
        weldProperty.solubleApplyRemove = 0;
    }

    if(ui->checkBox_4->isChecked()){
        weldProperty.wealdTilt = 1;
    }else {
        weldProperty.wealdTilt = 0;
    }


    int returnInfo;
    communicateNormalDelegate->changeWeldPropertConfigList(weldProperty,returnInfo);

    emit showErrorMsgBox(QString("保存成功！"));

//    this->hide();

}

void WeldPropertyDialog::on_comboBox_currentIndexChanged(int index)
{
    updateShowWeldInfo(index);
}
